<?php
	die('temporarily disabled');
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Country representation in the Hall of Cool</title>
<style>
td.lite {
        background-color: #D0D0D0;
}
td.dark {
        background-color: #A0A0A0;
}

td.old {
        background-color: #808080;
        color: #C0C0C0;
}

td.week2{
	background-color: #B0B0F0;
}

td.week {
        background-color: #A0E0A0;
}

td.day {
        background-color: #FFFFA0;
}

td.hour {
        background-color: #FFD060;
}

td.fifteen {
        background-color: #FF4040;
}

</style>
</head>
<body>
Countries best represented in the <a href="hallofcool.php">Hall of Cool</a> at <?php echo date("Y-m-d H:i:s"); ?><br />
<table>

<?php

require_once('database.inc.php');
require_once('track.inc.php');
$dbm = connect_db("dbm");

$sort = "perc";
$sort2 = "num";
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
	if ($_GET['s'] == 1) { $sort = "num"; $sort2 = "perc"; }
	else if ($_GET['s'] == 2) $sort = "activeusers";
}

?>
<tr><th>Flag<?php
?><th>Country <?php if ($sort !== "activeusers") echo "<a href=\"coolstats.php?s=2\">"; ?>(active users)<?php if ($sort !== "activeusers") echo "</a>";
?><th><?php if ($sort !== "num") echo "<a href=\"coolstats.php?s=1\">"; ?>Teams<?php if ($sort !== "num") echo "</a>";
?><th><?php if ($sort !== "perc") echo "<a href=\"coolstats.php?s=0\">"; ?>Country %<?php if ($sort !== "perc") echo "</a>";
?><th>Been chased<th>Been hosted<th>Latest update<!-- <th>Oldest update<th>Least up-to-date Team --><th>Import
<?php

if (!$query = mysql_query("SELECT leagueid, home, count(userid) num FROM flags_v2 WHERE checked = 1 GROUP BY leagueid, home", $dbm)) die (mysql_error());

$numhome = array();
$numaway = array();

while ($row = mysql_fetch_array($query)) {
	if ($row['home'] == 1) $numhome[$row['leagueid']] = $row['num'];
	else if ($row['home'] == 0) $numaway[$row['leagueid']] = $row['num'];
}

if (!$query = mysql_query("SELECT f.leagueid, englishname, leaguename, activeusers, count( * ) num, 100 * ( count( * ) / activeusers ) perc
FROM leagues_daily l
LEFT JOIN `flags_updated` f ON f.leagueid = l.leagueid
GROUP BY l.leagueid
HAVING f.leagueid IS NOT NULL
ORDER BY $sort DESC, $sort2 DESC", $dbm)) die (mysql_error());

while ($row = mysql_fetch_array($query)) {
	if (!isset($numhome[$row['leagueid']])) $numhome[$row['leagueid']] = 0;
	if (!isset($numaway[$row['leagueid']])) $numaway[$row['leagueid']] = 0;
	if (!$subquery = mysql_query("SELECT teamid, teamname, fetcheddate, UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(FetchedDate) diff FROM flags_updated WHERE leagueid = {$row['leagueid']} ORDER BY fetcheddate DESC LIMIT 1", $dbm)) die (mysql_error());
#	if (!$subquery2 = mysql_query("SELECT teamid, teamname, fetcheddate, UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(FetchedDate) diff FROM flags_updated WHERE leagueid = {$row['leagueid']} ORDER BY fetcheddate ASC LIMIT 1", $dbm)) die (mysql_error());
	$subrow = mysql_fetch_array($subquery);
#	$subrow2 = mysql_fetch_array($subquery2);
	$class = "";
	if ($subrow['diff'] > 14 * 86400) $class = "lite";
	else if ($subrow['diff'] > 7 * 86400) $class = "week2";
	else if ($subrow['diff'] > 86400) $class = "week";
	else if ($subrow['diff'] > 3600) $class = "day";
	else if ($subrow['diff'] > 900) $class = "hour";
	else $class = "fifteen";
/*	$class2 = "";
	if ($subrow2['diff'] > 14 * 86400) $class2 = "lite";
	else if ($subrow2['diff'] > 7 * 86400) $class2 = "week2";
	else if ($subrow2['diff'] > 86400) $class2 = "week";
	else if ($subrow2['diff'] > 3600) $class2 = "day";
	else if ($subrow2['diff'] > 900) $class2 = "hour";
	else $class2 = "fifteen";*/
	echo "<tr><td><img src=\"./flags/{$row['leagueid']}flag.gif\" border=\"1\"/><td><a href=\"hallofcool.php?leagueid={$row['leagueid']}\">{$row['leaguename']} / {$row['englishname']}</a> ({$row['activeusers']})<td>{$row['num']}<td>{$row['perc']}<td>{$numaway[$row['leagueid']]}<td>{$numhome[$row['leagueid']]}<td class=\"$class\">{$subrow['fetcheddate']}<td><a href=\"countryimport.php?l={$row['leagueid']}\">Import</a>\n";
//<!-- <td class=\"$class2\">{$subrow2['fetcheddate']}<td>{$subrow2['teamname']} (<a href=\"coolness.php?tid={$subrow2['teamid']}#submit\">update</a>)-->\n";
}
?>
</table><br /><br />

<?php
if (!$query = mysql_query("SELECT f.leagueid id, l.leagueid, englishname, leaguename, activeusers
FROM leagues_daily l
LEFT JOIN `flags_updated` f ON f.leagueid = l.leagueid
WHERE l.leagueid != 255
GROUP BY l.leagueid
HAVING id IS NULL
ORDER BY activeusers DESC", $dbm)) die (mysql_error());
if (mysql_num_rows($query) > 0) { ?>
Countries NOT represented in the Hall of Cool - if you are from one of these, <a href="coolness.php#submit">enter your team now</a>!<br />
<table>
<tr><th>Flag<th>Country<th>Active users<th>Import
<?php
	while ($row = mysql_fetch_array($query)) {
		echo "<tr><td><img src=\"./flags/{$row['leagueid']}flag.gif\" border=\"1\"/><td>{$row['leaguename']} / {$row['englishname']}<td>{$row['activeusers']}<td><a href=\"countryimport.php?l={$row['leagueid']}\">Import</a>\n";
	}
}
echo "</table>\n";
urchin();
?>

</body>
</html>
